#!/bin/bash

dl_path='/basic/node'
tarFile='node-16.9.0.tar.xz'
sourceFile='node-16.9.0'

dl_unzip() {
  mkdir -p $dl_path

  cd $dl_path || exit
  wget -O $tarFile https://nodejs.org/dist/v16.9.0/node-v16.9.0-linux-x64.tar.xz

  if [ -e $tarFile ]; then
    tar -xvf $tarFile
    mv node-v16.9.0-linux-x64 $sourceFile
    rm -rf $tarFile
    echo '下载并解压完毕'
  else
    echo '下载node压缩包失败'
    exit
  fi
}

register() {
  local profile
  profile='/etc/profile'
  cat <<'EOF' >>$profile

#set for nodejs
export NODE_HOME=/basic/node/node-16.9.0
export PATH=$NODE_HOME/bin:$PATH
EOF

  source /etc/profile
  echo '环境变量注册完毕'
}

dl_unzip
register
node -v
